var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');

var app = getApp();
Page({
    data: {
        username: '',
        password: '',
        code: '',
        loginErrorCount: 0,
        statusBarHeight: 0,
        countdown: 60, // 倒计时初始值
        isCounting: false // 是否正在倒计时
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        // 页面渲染完成
        const {statusBarHeight} = wx.getSystemInfoSync();
        this.setData({statusBarHeight});
    },
    onReady: function () {

    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏

    },
    onUnload: function () {
        // 页面关闭

    },
    navBack: function () {
        wx.navigateBack({})
    },
    accountLogin: function () {
        var that = this;

        if (this.data.username.length == 0) {
            wx.showToast({
                title: '手机号不能为空',
                icon: 'none'
            })
            return false;
        }
        // 验证手机号格式
        if (!util.isMobile(that.data.username)) {
            wx.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            })
            return false;
        }
        if (!this.data.code) {
            wx.showToast({
                title: '请输入验证码',
                icon: 'none'
            });
            return false;
        }

        wx.request({
            url: api.AuthManageLoginByPhone,
            data: {
                username: that.data.username,
                code: that.data.code
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res)
                if (res.data.errno == 0) {
                    that.setData({
                        loginErrorCount: 0
                    });
                    app.globalData.hasAdminLogin = true;
                    wx.setStorageSync('manageUserInfo', {
                        ...res.data.data.userInfo,
                        username: res.data.data.username,
                        data: res.data.data,
                    });
                    wx.setStorage({
                        key: "manageToken",
                        data: res.data.data.token,
                        success: function () {
                            // wx.switchTab({
                            //     url: '/pages/ucenter/index/index'
                            // });
                            wx.redirectTo({
                                url: '/pages/manageIndex/manageIndex'
                            });
                        }
                    });
                } else {
                    that.setData({
                        loginErrorCount: that.data.loginErrorCount + 1
                    });
                    app.globalData.hasAdminLogin = false;
                    // util.showErrorToast(res.data?.errmsg || '账户登录失败');
                    wx.showToast({
                        title: res.data?.errmsg || '账户登录失败',
                        icon: 'none'
                    })
                }
            }
        });
    },
    bindUsernameInput: function (e) {

        this.setData({
            username: e.detail.value
        });
    },
    bindPasswordInput: function (e) {

        this.setData({
            password: e.detail.value
        });
    },
    bindCodeInput: function (e) {

        this.setData({
            code: e.detail.value
        });
    },
    clearInput: function (e) {
        switch (e.currentTarget.id) {
            case 'clear-username':
                this.setData({
                    username: ''
                });
                break;
            case 'clear-password':
                this.setData({
                    password: ''
                });
                break;
            case 'clear-code':
                this.setData({
                    code: ''
                });
                break;
        }
    },
    sendCode: function () {
        let that = this;
        if (this.data.isCounting) return; // 如果正在倒计时，直接返回
        if (this.data.username.length == 0) {
            wx.showToast({
                title: '手机号不能为空',
                icon: 'none'
            })
            return false;
        }
        // 验证手机号格式
        if (!util.isMobile(that.data.username)) {
            wx.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            })
            return false;
        }

        wx.request({
            url: api.AuthRegisterCaptchaNew,
            data: {
                mobile: that.data.username
            },
            method: 'POST',
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.errno == 0) {
                    wx.showModal({
                        title: '发送成功',
                        content: '验证码已发送',
                        showCancel: false
                    });
                    that.setData({
                        isCounting: true
                    });
                    let interval = setInterval(function () {
                        that.setData({
                            countdown: that.data.countdown - 1
                        });
                        if (that.data.countdown <= 0) {
                            that.setData({
                                isCounting: false,
                                countdown: 60
                            });
                            clearInterval(interval);
                        }
                    }, 1000);
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.data.errmsg,
                        showCancel: false
                    });
                }
            }
        });
    },
})
